package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.StudentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "学生")
@RestController
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;

    @ApiOperation(value = "分页查询学生信息", notes = "分页查询学生信息", httpMethod = "GET")
    @GetMapping("/students/{page}/{size}/{name}/{grade}/{tel}/{collegeId}/{majorId}/{clazz}")
    public R<IPage<Student>> findAll(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String name, @PathVariable String grade, @PathVariable String tel, @PathVariable("collegeId") Integer collegeId, @PathVariable("majorId") Integer majorId, @PathVariable String clazz) {
        Page<Student> studentPage = new Page<>(page, size);
        IPage<Student> res = studentService.findPage(studentPage, name, grade, tel, collegeId, majorId, clazz);
        return R.<IPage<Student>>ok("分页查询学生").data(res);
    }

    @ApiOperation(value = "根据学生id查询学生信息", notes = "根据学生id查询学生信息", httpMethod = "GET")
    @GetMapping("/student/{id}")
    public R<Student> findById(@PathVariable("id") Integer id) {
        Student res = studentService.findById(id);
        if (res != null) {
            return R.<Student>ok("请求成功").data(res);
        } else {
            return R.logicError("查询的用户不存在");
        }
    }

    @ApiOperation(value = "根据学生id删除学生信息", notes = "根据学生id删除学生信息", httpMethod = "DELETE")
    @DeleteMapping("/student/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        studentService.deleteById(id);
        return R.ok("删除成功");
    }

    @ApiOperation(value = "修改密码", notes = "修改密码", httpMethod = "PUT")
    @PutMapping("/studentPWD")
    public R updatePwd(@RequestBody Student student) {
        studentService.updatePwd(student);
        return R.ok("密码更新成功");
    }

    @ApiOperation(value = "修改学生信息", notes = "修改学生信息", httpMethod = "PUT")
    @PutMapping("/student")
    public R update(@RequestBody Student student) {
        int res = studentService.update(student);
        if (res != 0) {
            return R.ok("更新成功");
        }
        return R.logicError("更新失败");
    }

    @ApiOperation(value = "添加学生信息", notes = "添加学生信息", httpMethod = "POST")
    @PostMapping("/student")
    public R add(@RequestBody Student student) {
        String acc = student.getAcc();
        Long countByAcc = studentService.findCountByAcc(acc);
        if (countByAcc == 0) {
            int res = studentService.add(student);
            if (res == 1) {
                return R.ok("添加成功").data(true);
            } else {
                return R.logicError("添加失败");
            }
        } else {
            return R.ok("账号已存在！").data(false);
        }



    }
}
